home *** CD-ROM | disk | FTP | other *** search
-
-
-
- ELEMENTARY COMPUTER PROGRAMMING
- Lesson 3
-
- Copyright 1990
- Castle Oaks Computer Services
- Post Office Box 36082
- Indianapolis, IN 46236-0082
-
- In the first two sessions some of the basics of computers were
- covered. In addition, the specifics of TRAC, a hypothetical
- computer, were also covered. Computer users very seldom program
- a computer in its own language. Occasionally, a user will create
- a short program or patch a larger program by using a utility that
- will facilitate doing so.
-
- Machine language can be very tedious as we have seen using TRAC
- which is fairly easy to use. Furthermore, it is difficult to
- make changes in machine language since coding is usually sequen-
- tial; thus, any addition means modifying most subsequent instruc-
- tions. The easiest way to add instructions to an existing ma-
- chine language program is to:
-
- replace one existing instruction with an unconditional branch to
- someplace in unused memory,
-
- at the new location put in the instruction you removed,
-
- add the new instructions,
-
- followed by an unconditional branch back to the next consecutive
- location after the the instruction you previously replaced.
-
- Suppose, for example, we have a program that reads two numbers,
- adds them and then displays the result. Figure III-1 illustrates
- the original code.
-
- -----------------------------------------------------------------
- 0010 401001 Read in A and B (also C, D, and E)
- 0011 001001 Load A
- 0012 011002 Add B
- 0013 031003 Store answer at C
- 0014 411001 Display result
- 0015 500000 Halt
- 9999 000010 End of code
-
- Figure III-1
- -----------------------------------------------------------------
-
- Now suppose that we wish to modify this program so that it adds
- four numbers instead of two. For such a short program, it is
- easy to rewrite the whole program. But to illustrate how a large
- program might be patched, consider Figure III-2.
-
-
-
-
-
- III-1
-
-
- In the modified program, we have replaced the add instruction at
- location 0012 with an unconditional branch to 1501. Then the
- instruction formerly at 0012 is re-entered at 1501, the new in-
- structions follow with a branch back to location 0013. In this
- case we also have to change the instruction at 0013. Note that
- the new code must be inserted before the end-of-code line.
-
- -----------------------------------------------------------------
- 0010 401000 Read in A and B (also C, D, and E)
- 0011 001001 Load A
- 0012 261501 Branch unconditionally to 1501
- 0013 031005 Store answer at E
- 0014 411001 Display result
- 0015 500000 Halt
- 1501 011002 Add B
- 1502 011003 Add C
- 1503 011004 Add D
- 1504 260013 Branch back
- 9999 000010 End of code
-
- Figure III-2
- -----------------------------------------------------------------
-
- Besides being hard to modify, machine code is designed for ma-
- chines, not people. In order to make programming more user-
- friendly, higher order languages have been developed. A program-
- mer writes his code in one of these languages and then he uses a
- program to translate this code into machine language.
-
- There are two categories of higher order languages. They are:
-
- COMPILERS - These programs take simple statements such as
- A = B + C and translates them into several machine language
- instructions. This is called a one-to-many translation. Exam-
- ples of higher order languages are BASIC, C, COBOL, FORTRAN,
- PASCAL, etc. These languages are almost totally computer inde-
- pendent. That is, a program written in one of these languages
- can be translated to the machine language of different kinds of
- computers.
-
- ASSEMBLERS - These programs provide only a one-to-one transla-
- tion. These programs are machine dependent and there must be a
- different assembler for each different type of computer. An
- example assembly language program fragment is:
-
- LD B
- AD C
- ST A
-
- A program written in a higher order language is called the source
- code and the resultant machine code, after translation, is called
- object code. Usually the translation is performed on the machine
- the code is going to run on; however, in some environments, the
- machine code is generated on a different computer. The program
- used is called a cross-compiler or cross-assembler. For example,
-
-
- III-2
-
-
- a program for a computer that is to be integrated into a machine
- (e.g. automobile, toaster, VCR, etc.) would be developed on a
- large computer before the code is inserted in the target comput-
- er.
-
- In this course, we will be discussing the assembly program for
- TRAC. It is called TRAP (TRaining computer Assembly Program).
- The primary purpose of this course is to introduce you to an
- assembly program. TRAP will provide a simple language for that
- purpose. After mastering TRAP, you should find it easier to
- learn other assemblers. When a training facility offers a course
- in "machine language" for a particular computer, it is usually a
- course in the assembly language used for that computer.
-
- TRAP Coding Format
-
- Figure III-3 shows the format required for TRAP.
-
- -----------------------------------------------------------------
-
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------------------------+
- |0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1 8|
- |1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7 0|
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------------------------+
- |Loc- | | |T|Op |Address| |Comments |
- | ation| | |a|co-| | | |
- | | | |g| de| | | |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------------------------+
-
- Figure III-3
- -----------------------------------------------------------------
-
- This format must be adhered to strictly except where noted other-
- wise. Note that the format for TRAP coding is very similar to
- that of TRAC. In fact, you may enter machine code in the TRAP
- format for use directly on TRAC. An explanation of the format is
- as follows:
-
- The first four columns are reserved for a memory location. This
- location may be entirely numeric, it may be mnemonic (i.e. it may
- be a symbolic location such as "XVEL"), or it may be blank. The
- very first line of code MUST have a numeric machine address. It
- specifies the machine origin of the code. A mnemonic address may
- be any combination of letters and numbers (at least one must be
- non-numeric). The mnemonic is case sensitive (although upper
- case only is recommended); and it is position sensitive. That
- is, the following mnemonic locations are all different: (a ),
- ( a ), ( a ), ( a), (A ), ( A ), ( A ), and ( A). More
- will be said about mnemonic locations later.
-
- Column 5 must always be blank.
-
- Columns 6 through 15 may be used for constants. A constant may
- be positioned anyplace within the field but it must not exceed
- nine digits plus sign.
-
-
- III-3
-
-
- Columns 6 through 8 must be blank on instructions.
-
- Column 9 is the tag field and is used just as in TRAC. That is,
- it may contain only a single digit, 1 through 9, or blank.
-
- Columns 10 and 11 are for the operation code. This field may
- contain either an actual numeric code or any one of the mnemonic
- codes shown in Lesson 1. A mnemonic code may be either upper or
- lower case but not both upper and lower case. Assembly programs
- usually contain some pseudo operation codes which are used as
- instructions to the assembler. TRAP has only one of these and it
- is EN. This signifies the ENd of code. There may be only one of
- these and it must be in the very last line of code. The EN
- instruction must contain, in the address field, the location
- where execution of the program is to begin. It will generate the
- 9999 line required by TRAC.
-
- Columns 12 through 15 are the address field. This field may
- contain an actual machine location, a mnemonic location, or a
- relative address. Any mnemonic address used in the address field
- must appear once and only once in the location field of some
- instruction or constant. A relative address is one that is
- defined relative to the location of the current instruction. A
- relative address must have an asterisk, "*", in column 12. It
- may be followed by a plus or minus sign and a one or two digit
- number. Thus, *+1 refers to the next location, * refers to the
- current location, and *-1 refers to the previous location.
- Although relative addresses of *+99 and *-99 are possible, it is
- recommended that relative addressing be confined to the near
- neighborhood of the current location.
-
- Column 16 must be blank.
-
- Columns 17 through 80 may be used for comments. They are carried
- over to the object code but are ignored otherwise. It is recom-
- mended that you not use beyond column 64 to avoid lines over 80
- columns in the object code.
-
- Figure III-4 gives a TRAP program of the problem coded earlier in
- Figure II-1.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- III-4
-
-
- -----------------------------------------------------------------
- 0010 RD X READ X,Y
- LD X LOAD X
- AD Y ADD Y
- ST ANS STORE SUM
- PC X PRINT ON CONSOLE
- LD X LOAD X
- SU Y SUBTRACT Y
- ST ANS STORE DIFFERENCE
- PC X PRINT ON CONSOLE
- HT*
- X 0
- Y 0
- ANS 0
- EN0010
-
- Figure III-4
- -----------------------------------------------------------------
-
- Note that in Figure III-4, three lines of coding had to be added
- to define locations for X, Y, ANS. This is to comply with the
- rule, "Every mnemonic location given in the address field must be
- defined once and only once in the location field." This coding
- will assign X, Y, and ANS to locations 0020, 0021, and 0022,
- respectively.
-
- TRAP does not relieve us of all the responsibility about machine
- locations. Note that in the example of Figure III-4 that the
- read instruction will cause values to be read into five locations
- starting at location X. Therefore, we must be sure that none of
- these are used for instructions or constants.
-
- As an example of how we might force the assembler to make the
- same assignments as we made in Figure II-1, see Figure III-5.
-
- -----------------------------------------------------------------
- 0010 RD X READ X,Y
- LD X LOAD X
- AD Y ADD Y
- ST ANS STORE SUM
- PC X PRINT ON CONSOLE
- LD X LOAD X
- SU Y SUBTRACT Y
- ST ANS STORE DIFFERENCE
- PC X PRINT ON CONSOLE
- HT*
- 0099 0 Dummy line to re-origin
- X 0
- Y 0
- ANS 0
- EN0010
-
- Figure III-5
- -----------------------------------------------------------------
-
-
-
- III-5
-
-
- The code of Figure III-5 will force X, Y, and ANS to use the
- locations 0100, 0101, and 0102, respectively. Furthermore, if we
- re-origin, the order does not have to be as shown in the above
- figure. We might choose to do it as shown in Figure III-6.
-
- -----------------------------------------------------------------
- 0099 0 Dummy line to origin program
- X 0
- Y 0
- ANS 0
- 0009 0 Dummy line to re-origin
- STRT RD X READ X,Y
- LD X LOAD X
- AD Y ADD Y
- ST ANS STORE SUM
- PC X PRINT ON CONSOLE
- LD X LOAD X
- SU Y SUBTRACT Y
- ST ANS STORE DIFFERENCE
- PC X PRINT ON CONSOLE
- HT*
- ENSTRT
-
- Figure III-6
- -----------------------------------------------------------------
-
- Note now that we have given a mnemonic name to the location where
- the execution of the program is to begin.
-
- Next we will show another program to illustrate the use of both
- mnemonic locations and relative addressing. Consider the flow
- chart of Figure III-7.
-
- -----------------------------------------------------------------
- |
- v
- -------------- -----------------
- / Read OP,X,Y /<-------------/ Print OP,X,Y,Z /
- -------------- -----------------
- | ^ ^
- v . | |
- / \ / \ | |
- +-------+ = / \ > / \ = +-------+ |
- | Z=X+Y |<--<OP:1 >-----><OP:2 >----->| Z=X-Y | |
- +-------+ \ / \ / +-------+ |
- | \./ \./ |
- | | < | not = |
- | | | |
- | | +------+ | |
- | +->| STOP |<-+ |
- | +------+ |
- | |
- +--------------------------------------------+
-
- Figure III-7
- -----------------------------------------------------------------
-
- III-6
-
-
- In this program we read in three numbers. If the first number is
- a "1", we add the other two numbers; if it is a "2", we subtract
- the third number from the second; any other code in the first
- number stops the program. Figure III-8 gives a TRAP coding.
-
- -----------------------------------------------------------------
- 0999 0 Origin
- OP 0
- X 0
- Y 0
- Z 0
- 0 Dummy included to reserve read space
- ONE 1 Constant one
- STRT RD OP Read OP, X, Y
- LD OP Load OP
- SU ONE Find difference for test
- BNSTOP If < 1 go to halt
- BZ ADD If = 1 go to add routine
- SU ONE If > 1 see if it is 2
- BZ SUB If = 2 go to subtract
- STOP HT0000 If not stop
- SUB LD X Load X
- SU Y Subtract Y
- ST Z Store at Z
- BUPRNT Go to print
- ADD LD X Load X
- AD Y Add Y
- ST Z Store at Z
- PRNT PC OP Print
- BUSTRT Go back to start
- ENSTRT End of code with starting location
-
- Figure III-8
- -----------------------------------------------------------------
-
- Note that in this example, the program was origined at 0999.
- This was done to force the read area to start at 1000. Also, a
- dummy line was added after the declaration of Z. This was to
- reserve space for the read. If this line were omitted, the
- constant, ONE, would be replaced by zero on the first execution
- of a read. The assembler was allowed to assign locations for the
- executable code consecutively with the area for variables and
- constants.
-
- When assembled, the code in Figure III-8a results.
-
-
-
-
-
-
-
-
-
-
-
-
- III-7
-
-
- -----------------------------------------------------------------
- 0999 0 0999 0 Origin
- 1000 0 OP 0
- 1001 0 X 0
- 1002 0 Y 0
- 1003 0 Z 0
- 1004 0 0 Dummy included to reserve read space
- 1005 1 ONE 1 Constant one
- 1006 401000 STRT RD OP Read OP, X, Y
- 1007 001000 LD OP Load OP
- 1008 021005 SU ONE Find difference for test
- 1009 241013 BNSTOP If < 1 go to halt
- 1010 251018 BZ ADD If = 1 go to add routine
- 1011 021005 SU ONE If > 1 see if it is 2
- 1012 251014 BZ SUB If = 2 go to subtract
- 1013 500000 STOP HT0000 If not stop
- 1014 001001 SUB LD X Load X
- 1015 021002 SU Y Subtract Y
- 1016 031003 ST Z Store at Z
- 1017 261021 BUPRNT Go to print
- 1018 001001 ADD LD X Load X
- 1019 011002 AD Y Add Y
- 1020 031003 ST Z Store at Z
- 1021 411000 PRNT PC OP Print
- 1022 261006 BUSTRT Go back to start
- 9999 001006 ENSTRT End of code with starting location
-
- Figure III-8a
- -----------------------------------------------------------------
- 0999 0 Origin
- OP 0
- X 0
- Y 0
- Z 0
- 0 Dummy included to reserve read space
- ONE 1 Constant one
- RD OP Read OP, X, Y
- LD OP Load OP
- SU ONE Find difference for test
- BN*+4 If < 1 go to halt
- BZ*+8 If = 1 go to add routine
- SU ONE If > 1 see if it is 2
- BZ*+2 If = 2 go to subtract
- HT0000 If not stop
- LD X Load X
- SU Y Subtract Y
- ST Z Store at Z
- BU*+4 Go to print
- LD X Load X
- AD Y Add Y
- ST Z Store at Z
- PC OP Print
- BU*-16 Go back to start
- EN*-17 End of code with starting location
-
- Figure III-9
- -----------------------------------------------------------------
- III-8
-
-
- Figure III-9 shows the same program as Figure III-8 but using
- relative addressing instead of named locations. When assembled,
- it will result in the same storage assignments as shown earlier
- in Figure III-8a.
-
- -----------------------------------------------------------------
- |
- v
- -----------
- / Read MAX /
- -----------
- |
- v
- +-------+ +------+
- | I = 0 | | STOP |
- +-------+ +------+
- | ^
- v |
- ------------ -----------------
- / Read X(I) /<--+ / Print MAX,S,S2 /
- ------------ | -----------------
- | | ^
- v | | >=
- +-----------+ | / \
- | I = I + 1 | | / \ <
- +-----------+ | <I:MAX>-----------+
- | | \ / |
- v | \./ |
- / \ | ^ |
- / \ < | | |
- <I:MAX>------+ +-----------+ |
- \ / | I = I + 1 | |
- \./ +-----------+ |
- | >= ^ |
- v | |
- +-------+ +-----------------------+ |
- | I = 0 | | S = S + X(I) | |
- | S = 0 |------------>| S2 = S2 + X(I) * X(I) |<-+
- |S2 = 0 | +-----------------------+
- +-------+
-
- Exercise III-1
- -----------------------------------------------------------------
-
- As exercises, re-code Exercises I-1 and II-1 in TRAP. Code
- Exercise III-1 in TRAP also. In Exercise III-1, the program
- first requests the total number of values that are to be read on
- subsequent reads. It then reads that number of values and stores
- them in an array. You must be sure to reserve enough memory for
- that array. It is suggested that the definition of X be the last
- one of the coding. That will allow all the remainder of memory
- to be available for the array. You may define it earlier but any
- subsequent code must be re-origined to non-conflicting locations.
- After the array has been loaded, it is scanned and the sum of the
- X's and the sum of the squares of the X's is found and displayed.
- When this program is run, choose values small enough to not cause
- any overflows.
- III-9